<template>
  <div class="box" ref="wrapper">
    <div>
      <!--当前城市-->
      <div class="listprops">
        <div class="title">您的位置</div>
        <div class="listcontent">
          <span>{{this.city}}</span>
        </div>
      </div>
      <!--热门城市-->
      <div class="listprops">
        <div class="title">热门城市</div>
        <div class="listcontent other">
          <span v-for="item of hotCity" :key="item.id" @click="handleClickCity(item.name)">{{item.name}}</span>
        </div>
      </div>
      <!--字母-->
      <div class="listprops" v-for="(item, key ) of cityList" :key="key" :ref="key">
        <div class="title">{{key}}</div>
        <div class="listcontent letter">
          <span v-for="inner of item" :key="inner.id" @click="handleClickCity(inner.name)">{{inner.name}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'Vuex'
export default {
  name: 'cityList',
  props: {
    cityList: Object,
    hotCity: Array,
    letter: String
  },
  methods: {
    handleClickCity (city) {
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
    // handleListCity () {
    //   this.$store.dispatch('')
    // }
  },
  computed: {
    ...mapState(['city'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        console.log(this.$refs)
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import "~@/assets/style/varible.styl"
  .box
    position absolute
    overflow hidden
    left 0
    right 0
    top 2.06rem
    bottom 0
    background transparent
    .listprops
      width 100%
      height auto
      overflow hidden
      .title
        line-height .7rem
        font-size .26rem
        background  $color-black2
        padding-left .1rem
      .listcontent
        box-sizing border-box
        width 100%
        height auto
        padding .1rem
        overflow hidden
        font-size .26rem
        span
          box-sizing border-box
          float left
          display block
          width 33.3333%
          padding 5px 8px
          border 1px solid $color-blue
          border-radius .1rem
          font-size .28rem
          text-align center
          color $color-blue
        &.other
          span
            box-sizing border-box
            float left
            display block
            width 30.3333%
            padding 5px 8px
            margin 8px 1%
            border 1px solid $color-black2
            border-radius .1rem
            font-size .28rem
            text-align center
            color $color-black4
        &.letter
          span
            box-sizing border-box
            float left
            display block
            width 100%
            padding 5px 8px
            margin 8px 0
            border-bottom 1px solid $color-black2
            border-left none
            border-top none
            border-right none
            border-radius .1rem
            font-size .28rem
            text-align left
            color $color-black4
</style>
